package cn.iocoder.yudao.module.infra.framework.file.core.client.local;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 本地文件客户端
 *
 * @author 芋道源码
 */
public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {

    public LocalFileClient(Long id, LocalFileClientConfig config) {
        super(id, config);
    }

    @Override
    protected void doInit() {
        // 补全风格。例如说 Linux 是 /，Windows 是 \
        if (!config.getBasePath().endsWith(File.separator)) {
            config.setBasePath(config.getBasePath() + File.separator);
        }
    }

    @Override
    public String upload(byte[] content, String path, String type) {
        // 执行写入
        String filePath = getFilePath(path);
        FileUtil.writeBytes(content, filePath);
        // 拼接返回路径
        return super.formatFileUrl(config.getDomain(), path);
    }

    @Override
    public String upload(byte[] content, String path, String fileName, String type) throws Exception {
        String filePath = getFilePath(path);
        String fileUrl = filePath + File.separator + fileName;
        FileUtil.writeBytes(content, fileUrl);
        return StrUtil.format("{}/admin-api/infra/file/{}/get/", config.getDomain(), getId());
    }

    @Override
    public Map<String, String> localUpload(byte[] content, String path, String fileName, String type) throws Exception {
        String filePath = getFilePath(path);
        String localFileName = UUID.fastUUID() + "." + FileUtil.getSuffix(fileName);
        String fileUrl = filePath + File.separator + localFileName;
        FileUtil.writeBytes(content, fileUrl);

        Map<String, String> map = new HashMap<>();
        map.put("url", StrUtil.format("{}/admin-api/infra/file/{}/get/", config.getDomain(), getId()));
        map.put("localFileName", localFileName);
        return map;

    }

    @Override
    public void delete(String path) {
//        String filePath = getFilePath(path);
        FileUtil.del(path);
    }

    @Override
    public byte[] getContent(String path) {
//        String filePath = getFilePath(path);
        return FileUtil.readBytes(path);
    }

    @Override
    public String getFilePath(String path) {
        return config.getBasePath() + path;
    }

    @Override
    public String getPath(String path) throws Exception {
        return path;
    }

    @Override
    public String moveFile(String oldFullPath, String newPath, String fileName) {
        String newFullPath = config.getBasePath() + newPath;
        String newFilePath = newFullPath + File.separator + fileName;
        if (StrUtil.equals(oldFullPath, newFilePath)) {
            return newFullPath;
        }
        FileUtil.move(new File(oldFullPath), new File(newFilePath), true);

        return newFullPath;
    }

    @Override
    public boolean exists(String path) throws Exception {
        return FileUtil.exist(path);
    }
}
